package com.ibeeking.found.job.config;

import com.ibeeking.found.job.annotation.EnableJob;
import com.ibeeking.gmc.job.core.executor.impl.XxlJobSpringExecutor;
import com.ibeeking.nematos.utils.data.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * xxl-job config
 *
 * @Author ibeeking
 */
@ConditionalOnBean(annotation = EnableJob.class)
@Configuration
public class XxlJobConfig {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${ibeeking.job.admin.addresses}")
    private String adminAddresses;

    @Value("${ibeeking.job.accessToken:}")
    private String accessToken;

    @Value("${ibeeking.job.executor.appname:}")
    private String appName;

    @Value("${ibeeking.job.executor.title}")
    private String title;

    @Value("${ibeeking.job.executor.address:}")
    private String address;

    @Value("${ibeeking.job.executor.ip:}")
    private String ip;

    @Value("${ibeeking.job.executor.port:0}")
    private int port;

    @Value("${ibeeking.job.executor.logpath:}")
    private String logPath;

    @Value("${logging.path:}")
    private String defaultPath;

    @Value("${ibeeking.job.executor.logretentiondays:-1}")
    private int logRetentionDays;


    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(StringUtils.isBlank(appName) ? applicationName : appName);
        xxlJobSpringExecutor.setTitle(title);
        xxlJobSpringExecutor.setAddress(address);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(StringUtils.isBlank(logPath) ? defaultPath : logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }
}